package com.ibt.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.ibt.entity.Login;
import com.ibt.util.ApplicationContext;

public class LoginDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	static Transaction tx;
	
	private static  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private   static Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}
	public static int insert(Login u)throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(u);
		session.getTransaction().commit();
		
		endOperations(session);
		return 0;
	}
	public  Login getLoginDet(String LoginName) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     Query query=session.createQuery("from Login WHERE UNAME=:LoginName");
				query.setString("LoginName", LoginName);
				Object ob=query.uniqueResult();
				Login u=(Login)ob;
		     tx.commit();
		     return u;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	
	}

	public  Login validatCredentials(String Login, String pass) throws Exception {
		
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			
			Query query = session.createQuery("from Login where username=:Login  and password=:pass");
			query.setString("Login", Login);
			query.setString("pass", pass);
			Login u = (Login) query.uniqueResult();
			return u;
		}catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
		
	}

	
	public Login getLoginLoginDetails(String email) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			 tx = session.beginTransaction();
			 Query query = session.createQuery("from Login where email_id=:email");
			 query.setString("email", email);
			 Login u = (Login) query.uniqueResult();
			 tx.commit();
			 return u;
		}catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public Login getLoginDetails(int busid,int Loginid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Login where business_id=:busid and id=:id");
			query.setInteger("busid", busid);
			query.setInteger("id", Loginid);
			Login u = (Login) query.uniqueResult();
			tx.commit();
			return u;
		}catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public List<Login> findAll(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("from Login where business_id=:busid");
			query.setInteger("busid", busid);
			List<Login> u = (List<Login>) query.list();
			tx.commit();
			return u;
		}catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}


}
